﻿using An_Im.Core;
using An_Im.UI;
using Microsoft.Office.Tools;
using System;
using System.Collections.Generic;
using PPT = Microsoft.Office.Interop.PowerPoint;

namespace An_Im
{
    /// <summary>
    /// 任务窗格组。
    /// </summary>
    internal class TaskPaneGroup
    {
        ///// <summary>
        ///// 本体动画库窗体。
        ///// </summary>
        //public CustomTaskPane AnMainPane { get; set; }

        /// <summary>
        /// 动画风暴编辑器。
        /// </summary>
        public CustomTaskPane AnStormPane { get; set; }

        /// <summary>
        /// 录制动画。
        /// </summary>
        public CustomTaskPane AnRecorderPane { get; set; }

        /// <summary>
        /// 宏视图。
        /// </summary>
        public CustomTaskPane AnMacroPane { get; set; }

        /// <summary>
        /// 命令视图。
        /// </summary>
        public CustomTaskPane AnCommandPane { get; set; }


        /// <summary>
        /// 滑块面板。
        /// </summary>
        public CustomTaskPane AnSliderPane { get; set; }

    }

    /// <summary>
    /// 任务窗格管理器。
    /// </summary>
    internal static partial class TaskPaneManager
    {
        private static CustomTaskPaneCollection customTaskPaneCollection;

        public static CustomTaskPaneCollection CustomTaskPaneCollection => customTaskPaneCollection;

        private static Dictionary<PPT.Presentation, TaskPaneGroup> taskPaneDic;

        /// <summary>
        /// 需要手动触发。不采用默认的构造函数。
        /// </summary>
        public static void Initialize(CustomTaskPaneCollection customTaskPaneColl)
        {
            customTaskPaneCollection = customTaskPaneColl;
            taskPaneDic = new Dictionary<PPT.Presentation, TaskPaneGroup>();

            LoadTaskPaneConfig();

            //anMainFrame = new AnImMainFrame();
            //anMainFrame.AnImMainControl.itemsControls_lv1.ItemsSource = CoreManipulator.TagGroupDictionary["默认模式"];
            //anStormFrame = new UI.AnImStormFrame();
            //anRecorderFrame = new UI.AnImRecorderFrame();

            //anMacroFrame = new UI.AnImMacroViewFrame();
            //anCommandFrame = new UI.AnImCommandViewFrame();
            //anCommandFrame.AnImCommandViewControl.itemsControl_lv1.ItemsSource = Command.CommandManager.DefaultCommandGroup.Values;
        }

        public static TaskPaneGroup GetTaskPaneGroup(PPT.Presentation presentation)
        {
            AddTaskPane(presentation);
            return taskPaneDic[presentation];
        }

        public static TaskPaneGroup GetActiveTaskPaneGroup()
        {
            AddTaskPane(CoreManipulator.App.ActivePresentation);
            return taskPaneDic[CoreManipulator.App.ActivePresentation];
        }

        /// <summary>
        /// 添加任务窗格。
        /// </summary>
        /// <param name="presentation"></param>
        public static void AddTaskPane(PPT.Presentation presentation)
        {
            if (taskPaneDic.ContainsKey(presentation)) return;
            
            //var mainControl = new AnImMainControl();
            //mainControl.itemsControls_lv1.ItemsSource = CoreManipulator.TagGroupDictionary["默认模式"];
            //var anMainFrame = AnImWPFElementControl.CreateControl(mainControl);

            var anStormFrame = AnImWPFElementControl.CreateControl(new AnImStormControl());

            var anRecorderFrame = AnImWPFElementControl.CreateControl(new AnImRecorderControl());

            var anMacroFrame = AnImWPFElementControl.CreateControl(new AnImMacroViewControl());

            var commandControl = new AnImCommandViewControl();
            commandControl.itemsControl_lv1.ItemsSource = Command.CommandManager.DefaultCommandGroup.Values;
            var anCommandFrame = AnImWPFElementControl.CreateControl(commandControl);

            var anSliderFrame = AnImWPFElementControl.CreateControl(new AnImSliderControl());

            //var anMainPane = customTaskPaneCollection.Add(anMainFrame, "An\'Im");
            var anStormPane = customTaskPaneCollection.Add(anStormFrame, "动画编辑");
            var anRecordPane = customTaskPaneCollection.Add(anRecorderFrame, "收藏夹");
            var anMacroPane = customTaskPaneCollection.Add(anMacroFrame, "宏");
            var anCommandPane = customTaskPaneCollection.Add(anCommandFrame, "命令");
            var anSliderPane = customTaskPaneCollection.Add(anSliderFrame, "滑块控制");

            //anStormPane.Width = 520;
            taskPaneDic.Add(presentation, new TaskPaneGroup()
            {
                //AnMainPane = anMainPane,
                AnStormPane = anStormPane,
                AnRecorderPane = anRecordPane,
                AnMacroPane = anMacroPane,
                AnCommandPane = anCommandPane,
                AnSliderPane = anSliderPane
            });
            //anMainPane.Visible = false;
            anStormPane.Visible = false;
            anRecordPane.Visible = false;
            anMacroPane.Visible = false;
            anCommandPane.Visible = false;
            anSliderPane.Visible = false;

            anStormPane.Width = _anStormPaneWidth;
            anRecordPane.Width = _anRecorderPaneWidth;
            anMacroPane.Width = _anMacroPaneWidth;
            anCommandPane.Width = _anCommandPaneWidth;
            anSliderPane.Width = _anSliderPaneWidth;
        }

        /// <summary>
        /// 删除任务窗格。
        /// </summary>
        public static void DeleteTaskPane(PPT.Presentation presentation)
        {
            if (!taskPaneDic.ContainsKey(presentation)) return;

            UpdateCurrentTaskPaneState(); // 删除前尝试更新状态。

            var taskPaneGroup = taskPaneDic[presentation];
            customTaskPaneCollection.Remove(taskPaneGroup.AnStormPane);
            customTaskPaneCollection.Remove(taskPaneGroup.AnRecorderPane);
            customTaskPaneCollection.Remove(taskPaneGroup.AnMacroPane);
            customTaskPaneCollection.Remove(taskPaneGroup.AnCommandPane);
            // customTaskPaneCollection.Remove(taskPaneGroup.AnMainPane);
            taskPaneDic.Remove(presentation);
        }
       
        /// <summary>
        /// 展示主界面。
        /// </summary>
        [Obsolete]
        private static void ShowMainPane()
        {
            var pres = CoreManipulator.App.ActivePresentation;
            var group = GetTaskPaneGroup(pres);
            // group.AnMainPane.Visible = true;
        }

        /// <summary>
        /// 展示动画编辑界面。
        /// </summary>
        public static void ShowStormPane()
        {
            var pres = CoreManipulator.App.ActivePresentation;
            var group = GetTaskPaneGroup(pres);
            group.AnStormPane.Visible = true;
            GlobalMessageStateUpdater.RaiseAnimationSequenceMessage();
        }

        /// <summary>
        /// 展示动画收藏界面。
        /// </summary>
        public static void ShowRecorderPane()
        {
            var pres = CoreManipulator.App.ActivePresentation;
            var group = GetTaskPaneGroup(pres);
            group.AnRecorderPane.Visible = true;
        }

        /// <summary>
        /// 展示动画收藏界面。
        /// </summary>
        public static void ShowRecorderWindow()
        {

        }

        /// <summary>
        /// 展示宏界面。
        /// </summary>
        public static void ShowMacroPane()
        {
            var pres = CoreManipulator.App.ActivePresentation;
            var group = GetTaskPaneGroup(pres);
            group.AnMacroPane.Visible = true;
        }

        /// <summary>
        /// 展示宏界面。
        /// </summary>
        public static void ShowCommandPane()
        {
            var pres = CoreManipulator.App.ActivePresentation;
            var group = GetTaskPaneGroup(pres);
            group.AnCommandPane.Visible = true;
        }

        /// <summary>
        /// 展示滑块面板。
        /// </summary>
        public static void ShowSliderPane()
        {
            var pres = CoreManipulator.App.ActivePresentation;
            var group = GetTaskPaneGroup(pres);
            group.AnSliderPane.Visible = true;
        }

    }



}
